/**
 *@description:TODO
 * @author: cai
 *@time:2017年10月19日上午10:05:55
 */
package com.pos.modules.cashier;

import android.content.res.Configuration;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.basewin.aidl.OnPrinterListener;
import com.basewin.services.ServiceManager;
import com.pos.BaseActivity;
import com.pos.R;
import com.pos.toolbox.FinancialUtils;
import com.zng.common.PrintPowerOnAndOffUtils;
import com.zng.common.PrintUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @description:支付成功
 * @author cai
 * @time:2017年10月19日上午10:05:55
 */
public class PaySuccessActivity extends BaseActivity implements OnClickListener {
	private TextView text_final_price;
	private Button button_print, button_continue;
	private String memberCode, memberName, memberLevel, memberScore, printInfo;
	private long totalPrice;

	private TextView text_common_title;
	private LinearLayout linear_title_back;
	private PrinterListener printer_callback = new PrinterListener();
	JSONObject printJson = new JSONObject();
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	// @Override
	// protected void onDestroy() {
	// // for (Activity activity : AppSet.cashierActivities) {
	// // activity.finish();
	// // }
	// // startActivity(CashierActivity.class);
	// setResult(RESULT_OK);
	// finish();
	// }

	@Override
	public void onBackPressed() {
		setResult(RESULT_OK);
		finish();
	}

	@Override
	public void initView() {
		setContentView(R.layout.pay_success_layout);
		text_final_price = bindId(R.id.text_final_price);
		button_print = bindId(R.id.button_print);
		button_continue = bindId(R.id.button_continue);
		text_common_title = bindId(R.id.text_common_title);
		linear_title_back = bindId(R.id.linear_title_back);

	}

	@Override
	public void initData() {
		// ActionBarManager.init(this, "收银完成", false, null, null);
		text_common_title.setText("收银完成");
		linear_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(RESULT_OK);
				finish();

			}
		});
	/*	memberCode = getIntent().getStringExtra("memberCode");
		memberName = getIntent().getStringExtra("memberName");
		memberLevel = getIntent().getStringExtra("memberLevel");
		memberScore = getIntent().getStringExtra("memberScore");*/
		printInfo = getIntent().getStringExtra("printInfo");
		totalPrice = getIntent().getLongExtra("totalPrice", 0);
		Log.e("TAG", "initData: "+totalPrice );
		text_final_price.setText("总计收款：¥" + FinancialUtils.fenToYuan(totalPrice) + "元");
		//startPrintAndOut();

	}

	@Override
	public void initListener() {
		button_print.setOnClickListener(this);
		button_continue.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			/**/
		case R.id.button_print:
			startPrintAndOut();

			break;
		case R.id.button_continue:
			setResult(RESULT_OK);
			finish();
			break;

		default:
			break;
		}

	}

	// private void startPrint() {
	// ServiceManager.getInstence().verifyData("2222222");
	// try {
	// try {
	// JSONArray printTest = new JSONArray();
	//
	// JSONObject json0 = new JSONObject();
	// json0.put("content-type", "txt");
	// json0.put("content", printInfo);
	// json0.put("size", "2");
	// json0.put("position", "left");
	// json0.put("offset", "0");
	// json0.put("bold", "0");
	// json0.put("italic", "0");
	// json0.put("height", "-1");
	//
	// printTest.put(json0);
	// printJson.put("spos", printTest);
	// // 设置底部空3行
	// // Set at the bottom of the empty 3 rows
	// com.pos.sdk.printer.PosPrinter.Parameters parameters =
	// ServiceManager.getInstence().getPrinter()
	// .getParameters();
	// parameters.setPrintGray(5000);
	// parameters.setPrintStep(4000);
	// parameters.setPrintHeatPoint(1000);
	// ServiceManager.getInstence().getPrinter().setParameters(parameters);
	// ServiceManager.getInstence().getPrinter().setPrintGray(2000);
	// ServiceManager.getInstence().getPrinter().printBottomFeedLine(5);
	// ServiceManager.getInstence().getPrinter().print(printJson.toString(),
	// null, printer_callback);
	//
	// } catch (JSONException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// }
/*
* 打印小票
*
* */
	private void startPrint() {
		PrintPowerOnAndOffUtils pp = new PrintPowerOnAndOffUtils(mContext);
		pp.powerOn();
		new Handler().postDelayed(new Runnable() {
			public void run() {

				new Thread(new Runnable() {
					@Override
					public void run() {
						PrintUtils printUtils = new PrintUtils(mContext);
						printUtils.detectionPrinter();
						printUtils.printTextAndNewLine(printInfo);
						Log.e("TAG", "run: "+printInfo );
						printUtils.setLineSpace(2);
						//打印条形码
						printUtils.printOneDemChar("1231241244");
						printUtils.printStepPass(40);
					}
				}).start();

			}
		}, 500);

	}

	private void startPrintAndOut() {

		{
			//	ServiceManager.getInstence().verifyData("2222222");
			try {
				try {
					JSONArray printTest = new JSONArray();

					JSONObject json0 = new JSONObject();
					json0.put("content-type", "txt");

		          	json0.put("content",printInfo );

					//	json0.put("content", "212123123");
					json0.put("size", "2");
					json0.put("position", "left");
					json0.put("offset", "0");
					json0.put("bold", "0");
					json0.put("italic", "0");
					json0.put("height", "-1");
					JSONObject json1 = new JSONObject();
					json1.put("content-type", "one-dimension");
					json1.put("content",getIntent().getStringExtra("orderCode"));
					json1.put("size", "3");
					json1.put("position", "left");
					json1.put("offset", "0");
					json1.put("bold", "0");
					json1.put("italic", "0");
					json1.put("height", "-1");
					printTest.put(json0);
					printTest.put(json1);
					printJson.put("spos", printTest);
					// 设置底部空3行
					// Set at the bottom of the empty 3 rows
					com.pos.sdk.printer.PosPrinter.Parameters parameters = ServiceManager.getInstence().getPrinter()
							.getParameters();
					parameters.setPrintGray(5000);
					parameters.setPrintStep(4000);
					parameters.setPrintHeatPoint(1000);
					ServiceManager.getInstence().getPrinter().setParameters(parameters);
					ServiceManager.getInstence().getPrinter().setPrintGray(2000);
					ServiceManager.getInstence().getPrinter().printBottomFeedLine(5);
					ServiceManager.getInstence().getPrinter().print(printJson.toString(), null, printer_callback);
				/*JSONObject jsonObject=new JSONObject();
				JSONArray printTest1 = new JSONArray();

				JSONObject json01 = new JSONObject();
				json0.put("content-type", "one-dimension");
				json0.put("content", "11123123");
				json0.put("size", "2");
				json0.put("position", "left");
				json0.put("offset", "0");
				json0.put("bold", "0");
				json0.put("italic", "0");
				json0.put("height", "-1");
				jsonObject.put("spos", printTest1);
				ServiceManager.getInstence().getPrinter().print(jsonObject.toString(), null, printer_callback);*/
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}










	}


	public class PrinterListener implements OnPrinterListener {

		@Override
		public void onStart() {
			// TODO 打印开始
			// Print start
			// Log.d(TAG, getStr(R.string.print_start));
			// sb.append(getStr(R.string.print_start) + "\n");
			// showText(sb);
			Log.e(Tag, "PrinterListener onStart");
		}

		@Override
		public void onFinish() {
			// TODO 打印结束
			// End of the print
			// Log.d(TAG, getStr(R.string.print_end));
			// sb.append(getStr(R.string.print_end) + "\n");
			// showText(sb);
			Log.e(Tag, "PrinterListener onFinish");

		}

		@Override
		public void onError(int errorCode, String detail) {
			// TODO 打印出错
			// print error
			// Log.d(TAG, getStr(R.string.print_error) + " errorcode = " +
			// errorCode + " detail = " + detail);
			// sb.append(getStr(R.string.print_error) + errorCode + " detail = "
			// + detail + "\n");
			// showText(sb);
			Log.e(Tag, "PrinterListener onError " + detail);

		}
	};
}
